﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CrashSimul8or
{
    class CircleParticleEngine : ParticleEngine
    {
        public float radius;
        public float circularVelocity;

        public CircleParticleEngine(List<Texture2D> textures, Vector2 location, float circularVelocity, float radius, int newParticlesPerUpdate)
            : base(textures, location, newParticlesPerUpdate)
        {
            this.radius = radius;
            this.circularVelocity = circularVelocity;
        }

        public void Update(Vector2 newLocation)
        {
            if (particles.Count <= 0)
            {
                GenerateNewParticles();
            }
            EmitterLocation = newLocation;
            foreach (Particle p in particles)
            {
                getIntensity(p);
                p.Position = newLocation;
            }

            for (int particle = 0; particle < particles.Count; particle++)
            {
                particles[particle].Update();
                if (particles[particle].TTL <= 0)
                {
                    particles.RemoveAt(particle);
                    particle--;
                }
            }
        }

        private void GenerateNewParticles()
        {
            for (int i = 1; i < newParticlesPerUpdate+1; i++)
            {
                particles.Add(new CircularParticle(textures[random.Next(textures.Count)],
                                EmitterLocation, circularVelocity, (float)(2 * Math.PI * i / newParticlesPerUpdate), radius, Color.Red, 0.5f, 100));
            }
        }

        private void getIntensity(Particle p)
        {
            float intensity = ((float)p.TTL / (float)p.LifeTime);
            Vector4 newColor = new Vector4();
            if (GameVariables.fadingParticles)
            {
                newColor.W = intensity;
            }
            else
            {
                newColor.W = 1.0f;
            }
            newColor.X = Color.Orange.R;
            newColor.Y = Color.Orange.G;
            newColor.Z = Color.Orange.B;

            p.Color = new Color(newColor);
        }
    }
}
